package leecode

func dailyTemperaturesLCR(temperatures []int) []int {
	stack := make([]int, 0)
	result := make([]int, len(temperatures))
	stack = append(stack, 0) //从栈低到栈顶 , 逐渐增大 , 递增栈
	for i := 1; i < len(temperatures); i++ {
		topIndex := stack[len(stack)-1] //栈顶元素的下标
		if temperatures[i] > temperatures[topIndex] {
			for len(stack) != 0 && temperatures[i] > temperatures[topIndex] {
				//栈顶元素出栈
				stack = stack[:len(stack)-1]
				result[topIndex] = i - topIndex
				if len(stack) != 0 {
					topIndex = stack[len(stack)-1]
				}
			}
			stack = append(stack, i)
		} else {
			stack = append(stack, i)
		}
	}
	return result
}
